package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.ISalesService;
import com.woniu.supermarket.vo.SalesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sales")
@Api(value = "销售单操作")
@Slf4j
public class SalesController {

    @Autowired
    ISalesService iSalesService;

    @GetMapping("/showSales")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<SalesVo>> showSales(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1")Integer now,
                                                          @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size,
                                                          @ApiParam(name = "salecode",value = "订单编码")@RequestParam(value = "salecode",defaultValue = "")String salecode){

        ResultResponse<PageInfo<SalesVo>> result = null;
//        System.out.println("salesCode=============="+salecode);
        try {
            PageInfo<SalesVo> pageInfo = iSalesService.findByCons(now, size, salecode);
            result = new ResultResponse<>(200,"查询销售订单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询销售订单失败");
        }
        return result;
    }

    @PutMapping("/reset")
    @ApiOperation(value = "重置订单操作")
    public ResultResponse<Void> reset(@RequestBody SalesVo sales){
        ResultResponse<Void> result = null;
        System.out.println("salesCode=============="+sales);
        try {
            iSalesService.reset(sales);
            result = new ResultResponse<>(200,"重置销售订单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"重置销售订单失败");
        }
        return result;
    }
}
